package larpplanner.gui;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.cloudgarden.resource.SWTResourceManager;

public class DateWin extends org.eclipse.swt.widgets.Dialog {

	private Timestamp timestamp;

	private int year_d;
	private int month_d;
	private int day_d;
	private int hour_d;
	private int minute_d;
	
	private Shell dialogShell;
	private DateTime calendar;
	private DateTime time;
	
	public DateWin(Shell parent, int style) {
		super(parent, style);
	}

	public Timestamp open() {
		Calendar c = Calendar.getInstance();
		return open(c.get(Calendar.YEAR),
				    c.get(Calendar.MONTH),
				    c.get(Calendar.DAY_OF_MONTH),
				    c.get(Calendar.HOUR_OF_DAY),
				    c.get(Calendar.MINUTE));
	}
	
	public Timestamp open(int year, int month, int day, int hours, int minutes) {
		year_d = year;
		month_d = month;
		day_d = day;
		hour_d = hours;
		minute_d = minutes;
        dialogShell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        SWTResourceManager.registerResourceUser(dialogShell);
        dialogShell.setLayout(new RowLayout());
        
        calendar = new DateTime(dialogShell, SWT.CALENDAR);
        calendar.setMonth(month);
        calendar.setYear(year);
        calendar.setDay(day);
        
        time = new DateTime(dialogShell, SWT.TIME);
        time.setSeconds(0);
        time.setMinutes(minutes);
        time.setHours(hours);
        
        time.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected (SelectionEvent e) {
            	minute_d = time.getMinutes();
            	hour_d = time.getHours();
            }
        });
        
        calendar.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	year_d = calendar.getYear();
            	month_d = calendar.getMonth();
            	day_d = calendar.getDay();
            }
        });
        
        dialogShell.layout();
        dialogShell.pack();            
        dialogShell.setText("Select date and time");
        
        Display display = dialogShell.getDisplay();
        Point p = display.getCursorLocation();
        p.x -= dialogShell.getSize().x / 2;
        dialogShell.setLocation(p);
        dialogShell.open();
        while (!dialogShell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        
        // convert the date and time into Timestamp
        try{
        	String str_date = year_d + " " + (month_d+1) + " " + day_d + " " + hour_d + " " + minute_d;
        	DateFormat formatter = new SimpleDateFormat("yyyy MM dd HH mm");
        	Date date = (Date)formatter.parse(str_date);
        	timestamp = new Timestamp(date.getTime());
        }catch(ParseException e){
        	System.out.println("Exception: " + e);
        }
        
        return timestamp;
	}
	
}
